package cn.webrx.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-05-26 13:07
 */
public class UserRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");
        String user = "root";
        String passwd = "123";

        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        //用户名认证
        if(!token.getUsername().equals(user)){
            return null;//抛出异常账号不存在
        }

        //密码认证需要 SimpleAuthenticationInfo("",passwd,"") 实现
        return new SimpleAuthenticationInfo("",passwd,"");
    }
}
